﻿using System;

using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Silvermoon.Controls.Interfaces;

namespace Silvermoon.Controls.Classes
{

    public class ControlCollection<T> : IList<T> where T : Control
    {
        private INotifyControlCollection notifyOwner;
        private Control owner;
        private List<T> controls;

        public T[] Controls { get { return controls.ToArray(); } }

        public ControlCollection(Control owner)
            : base()
        {
            this.owner = owner;
            this.notifyOwner = owner as INotifyControlCollection;
            controls = new List<T>();
        }

        protected virtual void OnAddControl(T c)
        {
            if (notifyOwner != null) notifyOwner.ControlAdded(c);
        }

        protected virtual void OnControlRemoved(T c)
        {
            if (notifyOwner != null)
            {
                notifyOwner.ControlRemoved(c);
            }
        }

        protected virtual void OnClear()
        {
            if (notifyOwner != null) notifyOwner.CollectionCleared();
        }

        #region IList<T> Members

        public int IndexOf(T control)
        {
            return controls.IndexOf(control);
        }

        public void Insert(int index, T control)
        {
            OnAddControl(control);
            controls.Insert(index, control);
        }

        public void RemoveAt(int index)
        {
            if (notifyOwner != null)
            {
                T c = controls[index];
                OnControlRemoved(c);
            }
            controls.RemoveAt(index);
        }

        public T this[int index]
        {
            get
            {
                return controls[index];
            }
            set
            {
                controls[index] = value;
            }
        }

        #endregion

        #region ICollection<Control> Members

        public virtual void Add(T control)
        {
            OnAddControl(control);
            controls.Add(control);
        }

        public virtual void Clear()
        {
            controls.Clear();
        }

        public bool Contains(T control)
        {
            return controls.Contains(control);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            controls.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return controls.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T control)
        {
            OnControlRemoved(control);
            return controls.Remove(control);
        }

        #endregion

        #region IEnumerable<Control> Members

        public IEnumerator<T> GetEnumerator()
        {
            return controls.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return controls.GetEnumerator();
        }

        #endregion
    }

    public class ControlCollection : ControlCollection<Control>
    {
        public ControlCollection(Control owner) : base(owner) { }
    }
}
